package com.yuzhyn.hiops.app.utils;

import com.yuzhyn.hiops.common.mapper.DbMapper;
import com.yuzhyn.hiops.common.entity.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.yuzhyn.azylee.core.datas.collections.ListTool;
import com.yuzhyn.azylee.core.datas.strings.NameFormat;
import com.yuzhyn.azylee.core.datas.strings.StringTool;
import com.yuzhyn.azylee.core.dbs.bases.DbTypeConst;
import com.yuzhyn.azylee.core.dbs.converts.DbColumnTypeConvert;
import com.yuzhyn.azylee.core.reflects.bases.ReflectTool;
import com.yuzhyn.azylee.core.reflects.models.ClassInfo;
import com.yuzhyn.azylee.core.reflects.models.FieldInfo;
import reactor.util.function.Tuple3;
import reactor.util.function.Tuples;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Component
public class DbUpdateTool {

    @Autowired
    DbMapper dbMapper;

    public void update() {

        log.info("********** 数据库升级 **********");
        log.info("** 数据库字段检查 **");
        List<ClassInfo> classInfoList = ReflectTool.reflect(
                new SysFile(),
                new SysFileBucket(),
                new SysFileCursor(),
                new SysFileDownloadLog(),
                new SysMachine(),
                new SysMachineStatusLog(),
                new SysMenu(),
                new SysRole(),
                new SysUser(),
                new SysUserFileConf()
        );
        // 字段信息转换
        List<Tuple3<String, String, String>> columnList = new ArrayList<>();
        if (ListTool.ok(classInfoList)) {
            for (ClassInfo item : classInfoList) {
                String tableName = NameFormat.camel2Underscore(item.getSimpleName());
                if (ListTool.ok(item.getFieldInfoList())) {
                    for (FieldInfo field : item.getFieldInfoList()) {
                        String columnName = NameFormat.camel2Underscore(field.getName());
                        String columnType = DbColumnTypeConvert.toColumnType(DbTypeConst.SQLITE, field.getType());
                        Tuple3<String, String, String> columnItem = Tuples.of(tableName, columnName, columnType);
                        columnList.add(columnItem);
                    }
                }
            }
        }


        if (ListTool.ok(columnList)) {
            for (Tuple3<String, String, String> item : columnList) {
                boolean updateFlag = false;
                try {
                    if (StringTool.ok(item.getT1(), item.getT2(), item.getT3())) {
                        dbMapper.addColumn(item.getT1(), item.getT2(), item.getT3());
                    }
                    updateFlag = true;
                } catch (Exception ex) {
                }
                if(updateFlag){
                    log.info("** [V] 数据库升级，表：" + item.getT1() + " ，字段：" + item.getT2() + " ，类型：" + item.getT3() + " **");
                }else{
                    // log.error("** [X] 数据库升级，表：" + item.getT1() + " ，字段：" + item.getT2() + " ，类型：" + item.getT3() + " **");
                }
            }
        }

        log.info("********** 数据库升级完成 **********");
    }
}
